package com.qingsong.mapper;

import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.entity.Dept;
import com.qingsong.entity.Permission;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface DeptMapper {
    @Insert("insert into ums_dept " +
            "(`title`, `info`, `fk_room_id`,   "  +
            "`deleted`, `created`, `updated` ) " +
            "values (#{title}, #{info}, #{fkRoomId},  " +
            "#{deleted},  #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Dept dept);

    @Select("select us.id, us.title, us.info, " +
            "us.deleted, us.created, us.updated from ums_dept us "+
            "where us.id = #{param1} and us.deleted = 0")
    Dept selectById(Long id);

    @Update("<script>" +
            "update ums_dept " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if>" +
            "<if test='info != null'> info = #{info}, </if>" +
            "<if test='fkRoomId != null'> fk_room_id = #{fkRoomId}, </if>" +
            "<if test='deleted != null'> deleted = #{deleted}, </if>" +
            "</set> " +
            "where id = #{id} and deleted = 0" +
            "</script>")
    int updateById(Dept dept);

    @Update("<script>" +
            "update ums_dept set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Update("update ums_dept set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Results(id = "resultMap", value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "fk_room_id", property = "fkRoomId"),
            @Result(column = "fk_room_id", property = "room",
                    one = @One(select = "com.qingsong.mapper.RoomMapper.selectById")),
    })
    @Select("select us.id, us.title, us.info, us.fk_room_id, " +
            "us.deleted, us.created, us.updated from ums_dept us "+
            "where us.deleted = 0")
    List<Dept> list();

    @Select("select us.id, us.title, us.info, " +
            "us.deleted, us.created, us.updated from ums_dept us "+
            "where us.deleted = 0")
    PageInfo<Dept> page(PageDTO pageDTO);

    @Select("select us.id, us.title, us.college, us.major, us.clazz, us.address, us.info, " +
            "us.deleted, us.created, us.updated from ums_dept us " +
            "where us.title = #{title} ")
    Dept selectByAllTitle(Dept dept);

    @Delete("delete from ums_dept where id = #{param1}")
    int deleteById(Long id);

    @Select("select us.id, us.title, us.info " +
            "us.deleted, us.created, us.updated from ums_dept us "+
            "where us.title = #{param1}")
    Dept selectByTitle(String title);


    @Select("select count(*) > 0 from ums_dept us " +
            "where us.fk_room_id = #{param1} and us.deleted = 0")
    boolean existsRoomId(Long fkRoomId);

    @Select("<script>" +
            "select count(*) > 0 from ums_dept us " +
            "where " +
            "<foreach item= 'e' collection = 'list' open = 'us.fk_room_id in (' close=')' separator=','> " +
            "#{e}" +
            "</foreach>" +
            "and us.deleted = 0 " +
            "</script>")
    boolean existsRoomIds(List<Long> fkRoomIds);

    @ResultMap("resultMap")
    @Select("select us.id, us.title, us.info, us.fk_room_id, " +
            "us.deleted, us.created, us.updated from ums_dept us "+
            "where us.deleted = 0 and us.title like concat('%', #{param1}, '%')")
    List<Dept> listLikeTitle(String title);

    @Select("select ud.title from ums_dept ud " +
            "inner join ums_emp ue on ud.id = ue.fk_dept_id " +
            "where ue.id = #{param1}")
    String selectByEmpFkDeptId(Long empId);




}
